package com.cloud.salon.client.authority.controller;

import com.cloud.salon.client.authority.service.RoleAuthorityService;
import com.cloud.salon.client.authority.service.RoleService;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.entity.authority.Role;
import com.cloud.salon.common.base.vo.RelationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 角色管理控制器
 * @Author wolf--LZH
 * @CreateDate 2019/4/10 0010 下午 1:30
 */
@RestController
@RequestMapping("role")
@Api(tags = "角色相关接口")
public class RoleController {

    private final String name = "角色";

    @Autowired
    private RoleService service;
    @Autowired
    private RoleAuthorityService roleAuthorityService;

    @PostMapping("setRoleAuthoritys")
    @ApiOperation(value="设置角色的权限")
    public Res<Role> setUserAuthoritys(@RequestBody RelationVO relationVO) {
        return roleAuthorityService.setRoleAuthoritys(relationVO.getBaseId(), relationVO.getRelationIdList());
    }

    @PostMapping("findById")
    @ApiOperation(value="通过id查找一个" + name)
    public Res<Role> findById(@RequestBody Integer id) {
        return service.findById(id);
    }

    @PostMapping("findByUser")
    @ApiOperation(value="通过用户获取对应的" + name)
    public Res<List<Role>> findByUser(@RequestBody Integer userId) {
        return service.findByUser(userId);
    }

    @PostMapping("findAll")
    @ApiOperation(value="获取所有的" + name)
    public Res<List<Role>> findAll() {
        return service.findAll();
    }

    @PostMapping("save")
    @ApiOperation(value="添加一个" + name)
    public Res<Role> save(@RequestBody Role role) {
        return service.save(role);
    }

    @PostMapping("saveAll")
    @ApiOperation(value="批量添加" + name)
    public Res<List<Role>> saveAll(@RequestBody List<Role> roles) {
        return service.saveAll(roles);
    }

    @PostMapping("update")
    @ApiOperation(value="修改" + name + "数据")
    public Res<Role> update(@RequestBody Role role) {
        return service.update(role);
    }

    @PostMapping("updateAll")
    @ApiOperation(value="批量修改" + name + "数据")
    public Res<List<Role>> updateAll(@RequestBody List<Role> roles) {
        return service.updateAll(roles);
    }

    @PostMapping("deleteById")
    @ApiOperation(value="通过id删除对应" + name)
    public Res<Boolean> deleteById(@RequestBody Integer id) {
        return  service.deleteById(id);
    }

    @PostMapping("deleteByIdAll")
    @ApiOperation(value="通过id集合批量删除" + name)
    public Res<Integer> deleteByIdAll(@RequestBody List<Integer> ids) {
        return service.deleteByIdAll(ids);
    }
}
